package com.wei.controller;


import com.wei.entity.GdRepairs;
import com.wei.mapper.GdRepairsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李泽伟
 * @since 2022-03-08
 */
@RestController
@RequestMapping
public class GdRepairsController {

    @Autowired
    private GdRepairsMapper repairsMapper;

    @RequestMapping("/getRepairs")
    public List<GdRepairs> getRepairs(){
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        return repairsMapper.selectByMap(map);
    }

    @RequestMapping("/addRepairs")
    public void addRepairs(@RequestParam Map<String,Object> repairsObj){
        GdRepairs repairs =setParam(repairsObj);
        repairsMapper.insert(repairs);
    }

    @RequestMapping("/updateRepairs")
    public void updateRepairs(@RequestParam Map<String,Object> repairsObj){
        GdRepairs repairs = setParam(repairsObj);
        repairsMapper.updateById(repairs);
    }

    @RequestMapping("/deleteRepairs")
    public void deleteRepairs(@RequestParam String repairsId){
        repairsMapper.deleteById(repairsId);
    }

    /**
     * 通过物业地址进行记录查询，用于报修信息的管理(用户)
     * @param address
     * @return
     */
    @RequestMapping("/getRepairsByAddress")
    public List<GdRepairs> getRepairsByAddress(@RequestParam String address){
        Map<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        map.put("property_address", address);
        return repairsMapper.selectByMap(map);
    }

    /**
     * 设置参数方法
     * @param repairsObj
     * @return
     */
    private GdRepairs setParam(@RequestParam Map<String, Object> repairsObj) {
        GdRepairs repairs=new GdRepairs();
        repairs.setRepairsId((String) repairsObj.get("repairsId"));
        repairs.setRepairsForeignkey((String) repairsObj.get("repairsForeignkey"));
        repairs.setRepairsDate((String) repairsObj.get("repairsDate"));
        repairs.setRepairsProblem((String) repairsObj.get("repairsProblem"));
        repairs.setRepairsSituation((String) repairsObj.get("repairsSituation"));
        repairs.setRepairsName((String) repairsObj.get("repairsName"));
        repairs.setRepairsFee(Double.parseDouble(repairsObj.get("repairsFee").toString()));
        repairs.setPropertyAddress((String) repairsObj.get("propertyAddress"));
        return repairs;
    }

}

